﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace LinkCard.CustomerView
{
    public partial class Search : System.Web.UI.Page
    {
        #region Variable declaration

        private int PageSize = 10;
        private int CurrentPage = 1;
        private int TotalPageCount = 1;

        public string SearchKey
        {
            get
            {
                string reval = "";
                if (ViewState["SearchKey"] != null)
                    reval = ViewState["SearchKey"].ToString();

                return reval;
            }
            set
            {
                ViewState["SearchKey"] = value;
            }
        }

        #endregion

        protected void Page_Load(object sender, EventArgs e)
        {
            if (IsPostBack) return;
            
            if (Request.Params["k"] != null)
                SearchKey = Request.Params["k"].ToString();
            if (Request.Params["Page"] != null)
                CurrentPage = Convert.ToInt32(Request.Params["Page"]);

            BindSearchResuiltPaged(SearchKey, CurrentPage, PageSize, ref TotalPageCount);

            Page.Title = SearchKey + " - Tìm kiếm - LinkCard";
            Page.MetaDescription = SearchKey + " - Tìm kiếm - LinkCard";
        }

        private void BindSearchResuiltPaged(string searchKey, int currentPage, int pageSize, ref int totalPages)
        {
            try
            {
                if (string.IsNullOrEmpty(searchKey))
                    return;

                Dao.DealDao db = new Dao.DealDao();
                var matchDeals = from dl in db.GetAll()
                                 where
                                     dl.Name.ToLower().Contains(searchKey.ToLower())
                                     || dl.Description.ToLower().Contains(searchKey.ToLower())
                                     || dl.Detail.ToLower().Contains(searchKey.ToLower())
                                 select dl;

                if (matchDeals == null || matchDeals.Count() == 0) return;

                rptSearchDeals.DataSource = matchDeals;
                rptSearchDeals.DataBind();
            
                // generate pager controls
                ltrPager.Text = Common.Pager.Create(CurrentPage, TotalPageCount);
            }
            catch (Exception)
            {

                throw;
            }
        }

        public string GetFirstDealImageFromAlbum(object album)
        {
            string reval = "";
            try
            {
                var imgList = Utils.Utilities.GetProductImage(album.ToString());
                if (imgList != null && imgList.Count > 0)
                    reval = imgList[0];
            }
            catch (Exception ex)
            {
                return "";
            }
            return reval;

        }

        public string GetSubString(object str, int length)
        {
            if (str == null)
                return "";

            else if (str.ToString().Length <= length)
                return HttpUtility.HtmlDecode(str.ToString());
            else
                return HttpUtility.HtmlDecode(str.ToString().Substring(0, length) + "...");

        }
    }
}